package com.briup.sms.web.controller;

import com.briup.sms.utils.Result;
import com.briup.sms.utils.UploadOSSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;

@RestController
@RequestMapping("/file")
@Api(tags = "文件管理")
public class FileController {
    private String dirPath = "D:\\briup-adam\\nginx-1.25.3\\html\\02-img\\";
    private String scheme="http://";
    private String nginxIP="127.0.0.1";
    private String nginxPort="80";
    private String nginxDir="02-img";
    @Autowired
    private UploadOSSUtils uploadOSSUtils;

//    @PostMapping("/upload")
////    public Result upload(MultipartFile file) throws IOException {
////        //获取唯一地 文件名 UUID 在单节点上 是唯一的
////        String fileName = UUID.randomUUID().toString();
//////        获取上传的文件名
////        String name = file.getOriginalFilename();
////        //获取上传的后缀名
////        String[] arr = name.split("[.]");
////        String suffixName = arr[arr.length - 1];
////        String path = dirPath + fileName + "." + suffixName;
////        file.transferTo(new File(path));
////        return Result.success(scheme+nginxIP+":"+nginxPort+"/"+nginxDir+"/"+fileName+"."+suffixName);
////    }
    @PostMapping("/upload")
    @ApiOperation("七牛云文件上传")
    public  Result upload(MultipartFile file) {
        String fileName = UUID.randomUUID().toString();
//        获取上传的文件名
        String name = file.getOriginalFilename();
//        //获取上传的后缀名
        String[] arr = name.split("[.]");
        String suffixName = arr[arr.length - 1];
        String fileUrl = uploadOSSUtils.uploadOss(file, fileName + "." + suffixName);
        return  Result.success(fileUrl);
    }
}
